package com.mccken.practice.top100.Top_1$20.Top_22;

import com.mccken.practice.top100.ListNode;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2024-01-24 14:12
 **/
public class IsPalindrome2 {

	private ListNode frontPointer;

	// 递归：利用堆栈里面存储的值
	public boolean isPalindrome2(ListNode head) {
		frontPointer = head;
		return recursivelyCheck(head);
	}

	private boolean recursivelyCheck(ListNode currentNode) {
		if (currentNode != null) {
			// 判断递归层
			if (!recursivelyCheck(currentNode.next)) {
				return false;
			}
			// 判断本层
			if (frontPointer.val != currentNode.val) {
				return false;
			}
			frontPointer = frontPointer.next;
		}
		return true;
	}

}
